package com.supermap;

import com.supermap.fabricsdk.FabricsdkUtils;
import com.supermap.fabricsdk.SmChain;
import com.supermap.fabricsdk.SmTestThreadWrite;
import com.supermap.ipfssdk.IpfsClient;

import java.io.File;
import java.util.Map;
import java.util.UUID;
import java.util.Vector;

/**
 * @author liqs
 * @version 1.0
 * @date 2020/8/6 17:15
 * 目标在 Linux 下运行跑
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {

        String blockchianchoose = args[0];
        if (blockchianchoose.equals("fabric")) {
            System.out.println("开始进行 Fabric TPS 测试");
            testFabricTps(args);
        } else if (blockchianchoose.equals("ipfs")) {
            System.out.println("开始进行 IPFS TPS 测试");
            testIPFSTps(args);
        }
    }

    //    java -classpath useFabricSdkJava-0.0.1-SNAPSHOT.jar com.supermap.Main fabric fabricnet.yaml 1 1000 true
    private static void testFabricTps(String[] args) {
        // 网络配置文件
        File networkFile = new File(args[1]);
        // 线程数
        int threadCount = Integer.valueOf(args[2]);
        // 每个线程执行数量
        int perThreadCount = Integer.valueOf(args[3]);
        // 是否使用国密
        String isGm = args[4];

//        File networkFile = new File("D:\\blockchain-network-config.yaml");
//        int threadCount = 3;
//        int perThreadCount = 2000;
//        String readKey = true;

        Map<String, Object> networkFileMap = FabricsdkUtils.getNetworkFileMap(networkFile);
        String channelName = FabricsdkUtils.getChannelName(networkFileMap);
        String chaincodeName = FabricsdkUtils.getChainCodeName(networkFileMap);

        SmChain.CryptoSuiteChoose cryptoSuiteChoose = SmChain.CryptoSuiteChoose.ECDSA;
        if (isGm.equals("true")) {
            cryptoSuiteChoose = SmChain.CryptoSuiteChoose.GM;
        }

        long starttime = System.currentTimeMillis();
        Thread[] threads = new Thread[threadCount];
        for (int i = 0; i < threadCount; i++) {
            SmChain chain = SmChain.getChain(channelName, networkFile, cryptoSuiteChoose);
            threads[i] = new SmTestThreadWrite(chain, chaincodeName, perThreadCount, i);
            threads[i].start();
        }
        // 保证线程执行完
        for (int i = 0; i < threads.length; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        long endtime = System.currentTimeMillis();
        System.out.println("总耗时 : " + (endtime - starttime) + "ms");
        System.out.println("数据量 : " + perThreadCount * threadCount);
        System.out.println(" TPS   : " + perThreadCount * threadCount / ((endtime - starttime) / 1000) + "");
    }

    //    java -classpath useFabricSdkJava-0.0.1-SNAPSHOT.jar com.supermap.Main ipfs 172.16.18.10 5001 40 2000
    private static void testIPFSTps(String[] args) throws InterruptedException {
        String ip = String.valueOf(args[1]);
        String port = String.valueOf(args[2]);
        // 线程数
        int threadCount = Integer.valueOf(args[3]);
        // 每个线程执行数量
        int perThreadCount = Integer.valueOf(args[4]);
        // 构造 ipfsClient
//        IpfsClient ipfsClient = new IpfsClient(ip, port);

        long starttime = System.currentTimeMillis();
        Vector<Thread> threads = new Vector<>();
        for (int i = 0; i < threadCount; i++) {
            IpfsClient ipfsClient = new IpfsClient(ip, port);
            IpfsThreadRead ipfsThreadRead = new IpfsThreadRead(ipfsClient, perThreadCount);
            Thread thread = new Thread(ipfsThreadRead);
            threads.add(thread);
            thread.start();
        }
        // 保证线程执行完
        for (Thread t : threads) {
            t.join();
        }
        long endtime = System.currentTimeMillis();
        System.out.println("总耗时 : " + (endtime - starttime) / 1000 + "s");
        System.out.println("总数据量 : " + threadCount * perThreadCount);
        System.out.println("TPS : " + threadCount * perThreadCount / ((endtime - starttime) / 1000) + "\n");

    }

    // 多线程测试IPFS的TPS
    static class IpfsThreadRead implements Runnable {

        private IpfsClient ipfsClientthread;
        private int sigle;

        public IpfsThreadRead(IpfsClient ipfsClient, int sigle) {
            this.ipfsClientthread = ipfsClient;
            this.sigle = sigle;
        }

        @Override
        public void run() {
            for (int i = 0; i < sigle; i++) {
                String result = this.ipfsClientthread.upBytes(UUID.randomUUID().toString().getBytes());
//                Assert.assertEquals(46, result.getBytes().length);
                if (result.getBytes().length != 46) {
                    System.out.println("Read data on chain FAILED! MESSAGE:" + result);
                }
            }
        }

    }

}
